$(document).ready(function() {
    edit_category();
    delete_category();
    var act_submit = false;
    $("#save_category").click(function() {
        if (act_submit === false) {
            act_submit = true;
            if ($("#category_name").val() != "") {
                $.post("ajax/updateCategory.php", {
                    category_id: $("#category_id").val(),
                    category_name: $("#category_name").val(),
                    category_parent: $("#parent_category_id").val()
                }, function(res) {
                    act_submit = false;
                    if (res.type == 0) {
                        $("#alert-success-category").html(res.msg).show();
                        hideError("#alert-success-category");
                        $("#category_name").val("");
                        
                        refresh_category_list();
                    } else {
                        $("#alert-error-category").html(res.msg).show();
                        hideError("#alert-error-category");
                    }
                }, "json");

            } else {
                act_submit = false;
                $("#alert-error-category").html("Vui lòng chọn tên category!").show();
                hideError("#alert-error-category");
                return false;
            }
        } else {
            return false;
        }
    });
    change_select_parent_category();
});
function change_select_parent_category() {
    $('.category_parent').unbind('change');
    $('.category_parent').on('change', function(e) {
        var select = $(this);
        var parent_html = $(this).parent();
        var parent_category_id = $(this).val();
        select.nextAll('select').remove();
        if (parent_category_id > 0) {
            $("#parent_category_id").val(parent_category_id);

            $.post("ajax/getCategoryListSelect.php", {
                category_id: $(this).val()
            }, function(res) {
                if (res.type == 0) {
                    parent_html.append(res.category_list);
                    change_select_parent_category();
                } else {
                    alert(res.msg);
                }
            }, "json");
        } else {
            
            parent_category_id = select.prev(".category_parent").val();
            if (parent_category_id) {
                $("#parent_category_id").val(parent_category_id);
            } else {
                $("#parent_category_id").val(0);
            }
        }
    });
}
function delete_category() {
    var act_del = false;
    $(".btn_delete_category").unbind('click');
    $(".btn_delete_category").click(function() {
        if (confirm("Are you sure you want to delete this category?")) {
            if (act_del === false) {
                act_del = true;
                $.post("ajax/deleteCategory.php", {
                    category_id: $(this).attr('rel')
                }, function(res) {
                    act_del = false;
                    alert(res.msg);
                    if (res.type == 0) {
                        refresh_category_list();
                    }

                }, "json");


            } else {
                return false;
            }
        } else {
            return false;
        }
    });
}
function edit_category() {
    var act_edit = false;
    $(".btn_edit_category").unbind('click');
    $(".btn_edit_category").click(function() {
        if (act_edit === false) {
            act_edit = true;
            $.post("ajax/getCategoryById.php", {
                category_id: $(this).attr('rel')
            }, function(res) {
                act_edit = false;
                $("#category_id").val(res.category_id);
                $("#category_name").val(res.category_name);
                $("#category_parent").html(res.show_select_category);
                 $("#parent_category_id").val(res.parent_category_id);
                change_select_parent_category();
                $(window).scrollTop(600);
            }, "json");


        } else {
            return false;
        }

    });
}
function refresh_category_list() {
    $.get("ajax/getCategoryList.php", function(res) {
        $("#tbody_category_list").html(res.html);
        $("#category_parent").html(res.category_list);
        $("#parent_category_id").val(0);
        $("#category_id").val(0)
        edit_category();
        delete_category();
        change_select_parent_category();
    }, "json");
}
function show_category_list() {
    var show_category_list = false;
    $(".parent_category_list").unbind('change');
    $('.parent_category_list').on('change', function(e) {
        if (show_category_list === false) {
            show_category_list = true;
            $.post("ajax/getSelectCategory.php", {
                category_id: $(this).attr('rel')
            }, function(res) {
                if (res.type == 0) {
                   
                }
                show_category_list = false;
            }, "json");

        }
    });
}


 